from osv import osv, fields


class kderp_tax_code(osv.osv):
    _name='kderp.tax.code'
    _desciption="Tax Code for Deduction"
    
    _columns={
             'name':fields.char('Description',size=250,required=True,select=1),
             'tax_percent':fields.float('Tax (%)',digits=(12,2),required=True,select=1),
             'locked':fields.boolean('Locked',required=True),
             'active':fields.boolean('Active')
             }
    _defaults = {
        'active':lambda *a:True,
        'tax_percent': lambda *a: 0.0,
        'locked':lambda *a:False
                }
    
    def write(self, cr, uid, ids, vals, context=None):
        tax_ids = ",".join(map(str,ids))
        cr.execute("Select id from kderp_tax_code where id in (%s) and locked=True" % tax_ids)
        if cr.fetchall():
            raise osv.except_osv("KDERP Error","Can't change, this code locked")
        osv.osv.write(self, cr, uid, ids,vals,context=context)
        return True
    
    def unlink(self, cr, uid, ids, context=None):
        kderp_tax_codes = self.read(cr, uid, ids, ['locked'])
        unlink_ids = []
        for s in kderp_tax_codes:
            if not s['locked']:
                unlink_ids.append(s['id'])
            else:
                raise osv.except_osv(('KDERP Error Action !'), ('Can delete Tax code, this code locked !'))
        return osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
kderp_tax_code()